package src;

import java.io.IOException;
import java.util.ArrayList;

public class Setup {
	
	Dictionary dict;
	Log log;
	int time;
	int node;
	TimeTable timeTable;
	
	public Setup() throws Exception {
		this.dict = new Dictionary();
		this.log = new Log(node);
		//CommUtil comm = new CommUtil();
		this.time = 0;
		this.node = 0;
		this.timeTable = new TimeTable();
	}
	

	
	public int getTime() {
		return this.time;
	}
	
	public int getNodeID() {
		return this.node;
	}
	
	public void insert(int key, int value) {
		int id = getNodeID();
		time++;
		timeTable.setClock(id, id, getTime());
		dict.insert(key, value);
		Event e = new Event("insert",timeTable.getClock(id,id,id),id,key,value);
		log.addEvent(e);
		
	}
	
	public boolean delete(int key) {
		int id = getNodeID();
		if (dict.list().containsKey(key)) {
			time++;
			timeTable.setClock(id, id, getTime());
			dict.delete(key);
			Event e = new Event("delete",timeTable.getClock(id, id, id),id,key);
			log.addEvent(e);
			return true;
		}
		return false;
	}
	
	public void send(String msg, int node, TimeTable timeTable, int key, int value) {
		String logString = "";

		for (int i=0; i<log.getSize(); i++) {
			if (log.getEvent(i).getTime() > timeTable.getClock(getNodeID(), node, getNodeID())) {


				logString += log.getEvent(i).toString(); 
			}
		}
		
	}
	
	public void executeEvents(ArrayList<Event> events) {
		for (int i=0; i<events.size(); i++) {
			Event e = events.get(i);
			if (e.getOperation().equals("insert")) {
				insert(e.getKey(), e.getValue());
			}
			if (e.getOperation().equals("delete")) {
				delete(e.getKey());
			}
		}	
	}
	
	public void mergeTimeTables(int sender, TimeTable otherTable) {
		for (int i=0; i<5; i++) {
			for (int j=0; j<5; j++) {
				if (timeTable.getClock(node,i,j)<otherTable.getClock(sender,i,j)) {
					timeTable.setClock(i, j, otherTable.getClock(sender,i,j));
				}
			}
		}
	}
	
	public static ArrayList<Event> parseMsg(String log) {
		ArrayList<Event> newEvents = new ArrayList<Event>();
		String op ="";
		int time = 0;
		int node = 0;
		int key = 0;
		int value = 0;
		String[] events = log.split(":");
		for (int i=0; i<events.length; i++) {
			String[] fields = events[i].split(",");
			op = fields[0];
			time = Integer.parseInt(fields[1]);
			node = Integer.parseInt(fields[2]);
			key = Integer.parseInt(fields[3]);
			if (fields.length>4) {
				value = Integer.parseInt(fields[4]);
				Event newEvent = new Event(op,time,node,key,value);
				newEvents.add(newEvent);
			} else {
				Event newEvent = new Event(op,time,node,key);
				newEvents.add(newEvent);

			}
		}
		
		System.out.println(events[0]+" "+events[1]);
//		System.out.println(newString[0]+" "+newString[1]+ " "+ newString[2]);
//		System.out.println(op+" "+ time+ " "+node);
		
		return newEvents;
		
	}
		

	
	
	public static void main(String[] args) {
		parseMsg("insert,1,2,2,3:insert,2,2,4,4");
	}
	
	
	
	
	
	

}
